# _*_ coding : utf-8 _*_
# @Time : 2024-01-30 0:02
# @Author : haowen
# @File : demo1
# @Project : pneumonia_tools
import os
import cv2
import math
import time
import numpy as np
from utils import stretch_linear, calc_convex_hull_2d, put_heatmap_2d
import warnings

# del warning
warnings.filterwarnings("ignore",message="invalid value encountered in cast")
np.seterr(divide="ignore")

def func():
    # -------------------------------
    # 计算2D凸包
    img = cv2.imread("../104.png", 0)
    start_t = time.time()
    # 对图像进行降采样
    img_arr = cv2.resize(img, (0, 0), fx=0.5, fy=0.5)
    img_arr = cv2.resize(img_arr, (0, 0), fx=0.5, fy=0.5)

    hull_arr = calc_convex_hull_2d(img_arr) / 255.0  # 每个scan获取2D凸包络区域
    end_t = time.time()
    print("Time cost(calc_convex_hull_2d and image_processing): %f s. " % (end_t - start_t))

    # 计算2D凸包的轮廓
    start_t = time.time()
    cov_arr = hull_arr.sum() / 4.0 * np.eye(2)
    y_arr, x_arr = np.where(cv2.Canny(hull_arr.astype(np.uint8) * 255, 50, 150) == 255)  # 2D凸包络区域获取轮廓像素
    end_t = time.time()
    print("Time cost(Canny): %f s. " % (end_t - start_t))

    # 构建2D高斯热力图
    start_t = time.time()
    for idx in range(y_arr.shape[0]):
        mu_arr = np.array([[x_arr[idx]], [y_arr[idx]]])
        # 2D凸包络区域轮廓像素构建2D高斯热力图
        hull_arr = put_heatmap_2d(hull_arr, mu_arr, cov_arr)
    end_t = time.time()
    # 将降采样后的图像复原为原始尺寸
    hull_arr = cv2.resize(hull_arr, (img.shape[1], img.shape[0]))
    print("Time cost(put_heatmap_2d): %f s. " % (end_t - start_t))
    cv2.imwrite("104_hull.png", stretch_linear(hull_arr))
    # -------------------------------

func()